package com.ctg.itrdc.imageconsole.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.data.dao.IDockerAppImageDao;
import com.ctg.itrdc.imageconsole.data.dto.DMSReturnImageDTO;
import com.ctg.itrdc.imageconsole.data.entity.DockerAppImage;
import com.ctg.itrdc.imageconsole.service.IDockerAppImageService;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;
import com.ctg.itrdc.paspsdk.data.dao.IBaseDao;

@Component("dockerAppImageService")
public class DockerAppImageServiceImpl implements IDockerAppImageService{

	@Resource
	private IBaseDao baseDao;
	
	@Resource
	private IDockerAppImageDao dockerAppImageDao;

	@Override
	public boolean deleteByDockerImageFileId(Long dockerImageFileId) {
		return dockerAppImageDao.deleteByDockerImageFileId(dockerImageFileId);
	}
	
	@Override
	public int deleteByDockerAppImage(DockerAppImage dockerAppImage) {
		return dockerAppImageDao.deleteByPrimaryKey(dockerAppImage);
	}
	
	
	/*
	 * 			Author			Date			Operation
	 * 			Shizhu.Peng		2017-03-06		create
	 */
	@Override
	public PageInfo<DMSReturnImageDTO> getImageFileDTO(Long userId, Integer pageNum, Integer pageSize) 
	{
		String sql = "select T1.id as id, T1.last_update_time as lastUpdateTime, T2.image_name as imageName from" + 
				" (select * from docker_image_file where create_user_id = ? and state = ?) as T1" + 
				" inner join" +
				" (select docker_image_index.docker_image_file_id as docker_image_file_id, docker_image_index.image_name as image_name" +
					" from docker_image_index inner join docker_micro_image" + 
					" on docker_image_index.id = docker_micro_image.docker_image_index_id group by docker_image_index.docker_image_file_id) as T2" +
				" on T1.id = T2.docker_image_file_id";
		List<Object> params = new ArrayList<>();
		params.add(userId);		
		params.add("001");	// 001 means successfully build, 002 failure
		return baseDao.jdbcFindPageInfo(sql, DMSReturnImageDTO.class, params, pageNum, pageSize);
	}
}
